#include "nr_net_command.h"

/**
 * @brief Convert a serializable nr_object to memory
 */
void nr_net_helper::object_to_byte_array(nr_object &object, QByteArray &byte_array)
{
    byte_array.reserve(50*1024);
    QDataStream data_stream(&byte_array, QIODevice::ReadWrite);
    object.serialize(data_stream);
}

/**
 * @brief Convert a serializable nr_object to memory
 */
QByteArray nr_net_helper::object_to_byte_array(nr_object &object)
{
    QByteArray byte_array;
    byte_array.reserve(50*1024);
    QDataStream data_stream(&byte_array, QIODevice::ReadWrite);
    object.serialize(data_stream);
    
    return byte_array;
}

/**
 * @brief Convert memory to nr_object
 */
void nr_net_helper::byte_array_to_object(QByteArray &byte_array, nr_object &object)
{
    QDataStream data_stream(&byte_array, QIODevice::ReadWrite);
    object.deserialize(data_stream);
}

/**
 * @brief Create the nr_object by command type
 */
nr_object* nr_net_helper::create_command_by_type(nr_net_command_type cmd_type, bool is_request)
{
    nr_object* obj = NULL;
    switch(cmd_type)
    {
    case NR_NET_COMMAND_LOGIN:
        if (is_request) obj = new nr_request_login;
        else obj = new nr_reply_login;        
        break;
    case NR_NET_COMMAND_LOGOUT:
        if (is_request) obj = new nr_request_logout;
        else obj = new nr_reply_logout;
        break;
    case NR_NET_COMMAND_GET_ITEMS:
        if (is_request) obj = new nr_request_get_items;
        else obj = new nr_reply_get_items;
        break;
    case NR_NET_COMMAND_START_DATA:
        if (is_request) obj = new nr_request_start_data;
        else obj = new nr_reply_start_data;
        break;
    case NR_NET_COMMAND_STOP_DATA:
        if (is_request) obj = new nr_request_stop_data;
        else obj = new nr_reply_stop_data;
        break;
    case NR_NET_COMMAND_QUERY_DATA:
        if (is_request) obj = new nr_request_query_data;
        else obj = new nr_reply_query_data;
        break;
    default: 
        Q_ASSERT(false);
    }
    
    return obj;
}

void nr_request_login::serialize(QDataStream& data_stream)
{
    data_stream << m_username;
    data_stream << m_password;

    nr_object::serialize(data_stream);
}

void nr_request_login::deserialize(QDataStream& data_stream)
{
    data_stream >> m_username;
    data_stream >> m_password;

    nr_object::deserialize(data_stream);
}

void nr_reply_login::serialize(QDataStream& data_stream)
{
    data_stream << m_cookie;
    
    nr_object::serialize(data_stream);
}

void nr_reply_login::deserialize(QDataStream& data_stream)
{
    data_stream >> m_cookie;

    nr_object::deserialize(data_stream);
}

void nr_reply_get_items::serialize(QDataStream& data_stream)
{
    m_item_server.serialize(data_stream);
    
    nr_object::serialize(data_stream);
}

void nr_reply_get_items::deserialize(QDataStream& data_stream)
{
    m_item_server.deserialize(data_stream);

    nr_object::deserialize(data_stream);
}

void nr_request_start_data::serialize(QDataStream& data_stream)
{
    // Save the count 
    data_stream << m_id_list.count();
    
    // Save each id
    foreach(nr_device_id_t device_id, m_id_list)
    {
        data_stream << device_id;
    }
}

void nr_request_start_data::deserialize(QDataStream& data_stream)
{
    // Get the count
    int count = 0;
    data_stream >> count;

    // Get each device id
    for(int i = 0; i < count; ++i)
    {
        QString device_id;
        data_stream >> device_id;

        m_id_list.push_back(device_id);
    }

}

void nr_reply_start_data::serialize(QDataStream& data_stream)
{
    // Save the count 
    data_stream << m_failed_id_list.count();

    // Save each id
    foreach(nr_device_id_t device_id, m_failed_id_list)
    {
        data_stream << device_id;
    }

}

void nr_reply_start_data::deserialize(QDataStream& data_stream)
{
    // Get the count
    int count = 0;
    data_stream >> count;

    // Get each device id
    for(int i = 0; i < count; ++i)
    {
        QString device_id;
        data_stream >> device_id;

        m_failed_id_list.push_back(device_id);
    }
}

void nr_request_stop_data::serialize(QDataStream& data_stream)
{
    // Save the count 
    data_stream << m_id_list.count();

    // Save each id
    foreach(nr_device_id_t device_id, m_id_list)
    {
        data_stream << device_id;
    }
}

void nr_request_stop_data::deserialize(QDataStream& data_stream)
{
    // Get the count
    int count = 0;
    data_stream >> count;

    // Get each device id
    for(int i = 0; i < count; ++i)
    {
        QString device_id;
        data_stream >> device_id;

        m_id_list.push_back(device_id);
    }    
}


void nr_request_query_data::serialize( QDataStream& data_stream )
{
    // Save the id 
    data_stream << this->id();

    // Save time ,the search condition
    data_stream << this->begin_time();
    data_stream << this->end_time();
}

void nr_request_query_data::deserialize( QDataStream& data_stream )
{
    // Get the id
    data_stream >> this->id();

    // Get time.
    data_stream >> this->begin_time();
    data_stream >> this->end_time();
}

void nr_reply_query_data::serialize( QDataStream& data_stream )
{
    // Save the count 
    data_stream << m_data_list.count();

    // Save each nr_data
    foreach(nr_data* data, m_data_list)
    {
        data->serialize(data_stream);
    }
}

void nr_reply_query_data::deserialize( QDataStream& data_stream )
{
    // Get the count
    int count = 0;
    data_stream >> count;

    // Get each nr_data
    for(int i = 0; i < count; ++i)
    {
        nr_data* data = nr_data::create();
        data->deserialize(data_stream);

        m_data_list.push_back(data);
    }
}
